//
//  UIImage+Extension.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/26.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import Foundation


extension UIImage {
    
    /**
     压缩图片到指定宽度
     */
    func scaleToWidth(width: CGFloat) -> UIImage {
    
        if self.size.width < width{
        
            return self
        }
        
        let height = width/self.size.width * self.size.height
        
        let rect = CGRectMake(0, 0, width, height)
        
        //开启指定上下文
        UIGraphicsBeginImageContext(rect.size)
        
        //画图
        self.drawInRect(rect)
        
        //取出图片
        let res = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭上下文
        UIGraphicsEndImageContext()
        
        //返回结果
        return res    
    }
    
 
    /**
    *  截图
    */
    class func getScreenShot() -> UIImage {
    
    let window = UIApplication.sharedApplication().keyWindow!
    let rect = window.frame
    
    //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 1)
    
    //将内容渲染到图形上下文
    window.drawViewHierarchyInRect(rect, afterScreenUpdates: true)
    
    //获取图片
    let img = UIGraphicsGetImageFromCurrentImageContext()
    
    //关闭图形上下文
    UIGraphicsEndImageContext()
    
    //返回图片
    return img
    }

    }
    
